#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from DV.gpgpu.trees.instruction_tree import Zicsr_instructions
from base.Sequence import Sequence
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU


class MainSequence(Sequence):
    def generate(self, **kargs):

        # Check the fflags
        index = self.getRegisterIndex("fflags")
        if index != 0x1:
            self.error("Index value for fflags should always be 0x1")
        reloadValue = self.getRegisterReloadValue("fflags")
        self.notice(">>>>>>>>>>>>   The fflags reload value is {:16X}".format(reloadValue))

        # Check the frm
        index = self.getRegisterIndex("frm")
        if index != 0x2:
            self.error("Index value for frm should always be 0x2")
        reloadValue = self.getRegisterReloadValue("frm")
        self.notice(">>>>>>>>>>>>   The frm reload value is {:16X}".format(reloadValue))

        # Check the fcsr
        index = self.getRegisterIndex("fcsr")
        if index != 0x3:
            self.error("Index value for fcsr should always be 0x3")
        reloadValue = self.getRegisterReloadValue("fcsr")
        self.notice(">>>>>>>>>>>>   The fcsr reload value is {:16X}".format(reloadValue))

        # Randomly generate instructions that access the fcsr and its fields
        # csr addresses for fcsr:  0x1=fflags, 0x2=frm, 0x3=fcsr
        fcsr_addresses = [0x1, 0x2, 0x3]
        # create a random list of csr access instructions
        list_of_csr_instructions = self.sample(list(Zicsr_instructions.keys()), 2)

        for instr in list_of_csr_instructions:
            csr_address = self.choice(fcsr_addresses)
            self.genInstruction(instr, {"csr": csr_address})


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
